package mysql

import (
	"context"
	"data-transfer/domain"
	"data-transfer/global"
	"go.uber.org/zap"
	"sync/atomic"
)

func (mdb *db) ClearAnnouncement() {

	deleteSql := "DELETE FROM `ANNOUNCEMENT` WHERE DECLAREDATE < DATE_SUB(CURDATE(), INTERVAL 1 YEAR) and ID > 0" //清理小于一年的数据
	result, err := mdb.db.Exec(deleteSql)
	if err != nil {
		global.LOG.Error("公告数据清理定时任务发生错误:", zap.Any("err", err))
	}
	affected, _ := result.RowsAffected()

	global.LOG.Info("公告数据清理定时任务执行成功:", zap.Int64("rowsAffected", affected))

}

func (mdb *db) GetAnnouncementById(ctx context.Context, id int64) (*domain.ANNOUNCEMENT, error) {
	var results []*domain.ANNOUNCEMENT
	err := mdb.db.NewSession().Where("ID=? ", id).Desc("ID").Find(&results)
	if err != nil {
		return nil, err
	}
	if len(results) > 0 {
		return results[0], nil
	}
	return nil, nil
}

//GetAnnouncements 取得所有的ANNOUNCEMENT 取得所有的ANNOUNCEMENT
func (mdb *db) GetAnnouncements(ctx context.Context) ([]*domain.ANNOUNCEMENT, error) {
	var results []*domain.ANNOUNCEMENT
	err := mdb.db.NewSession().Where("ID > 0 ").Desc("ID").Find(&results)
	return results, err
}

//FixAnnouncements 补充公告数据
func (mdb *db) FixAnnouncements(ctx context.Context, rows []*domain.ANNOUNCEMENT) error {
	var datas []*domain.ANNOUNCEMENT
	//先判断F10本地数据源是否有Announcemtid的数据,过滤掉已经已经存在的数据
	for _, r := range rows {
		has, err := mdb.HasAnnouncement(r.Announcemtid)
		if err != nil {
			global.LOG.Error("根据 SourceKey 判断 相应快讯新闻是否存在出错:", zap.Any("err", err))
			return err
		}
		//过滤掉已经存在的数据
		if !has {
			datas = append(datas, r)
		}
	}

	if len(datas) > 0 {
		announceNums, err1 := mdb.InsertAnnouncement(ctx, datas)
		if err1 != nil {
			global.LOG.Fatal("补充公告数据失败！", zap.Any("err", err1))
			return err1
		}
		global.LOG.Info("补充公告新增数据条数:", zap.Int64("nums", announceNums))
	}

	return nil
}

// InsertAnnouncement 新增公告数据
func (mdb *db) InsertAnnouncement(ctx context.Context, rows []*domain.ANNOUNCEMENT) (int64, error) {
	var affected int64
	var err error
	execFun := func(index int) {
		r := rows[index]
		aff, e := mdb.db.NewSession().Insert(r)
		if e != nil {
			err = e
		}
		atomic.AddInt64(&affected, aff)
	}
	DoBatch(len(rows), 20, execFun)

	return affected, err
}

// InsertAnnouncementSingle 新增公告数据，用户gocanal监控
func (mdb *db) InsertAnnouncementSingle(insertSql string, insertedMap map[string]interface{}) (int64, error) {
	var err error
	result, err := mdb.db.Exec(insertSql, insertedMap["ANNOUNCEMTID"], insertedMap["COMPCODE"], insertedMap["SYMBOL"], insertedMap["ANNTITLE"], insertedMap["ANNTEXT"], insertedMap["DECLAREDATE"], 0, 0, 0, insertedMap["EXCHANGE"], insertedMap["FILELINK"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	//affected, err := result.LastInsertId()
	return affected, err
}

// UpdateAnnouncement 更新公告信息，用于gocanal监控
func (mdb *db) UpdateAnnouncement(updateSql string, updateMap map[string]interface{}) (int64, error) {
	var err error
	result, err := mdb.db.Exec(updateSql, updateMap["ANNTITLE"], updateMap["ANNTEXT"], updateMap["DECLAREDATE"], updateMap["FILELINK"], updateMap["COMPCODE"], updateMap["ANNOUNCEMTID"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err
}

// DeleteAnnouncement 删除公告信息
func (mdb *db) DeleteAnnouncement(deleteSql string, deleteMap map[string]interface{}) (int64, error) {
	var err error
	result, err := mdb.db.Exec(deleteSql, deleteMap["COMPCODE"], deleteMap["ANNOUNCEMTID"])
	if err != nil {
		return 0, err
	}
	affected, err := result.RowsAffected()
	return affected, err
}

//HasAnnouncement 是否存在相应HasAnnouncement的公告信息
func (mdb *db) HasAnnouncement(announcemtId int) (bool, error) {
	news := new(domain.ANNOUNCEMENT)
	has, err := mdb.db.NewSession().Where("ANNOUNCEMTID=? ", announcemtId).Get(news)
	return has, err
}
